package com.android.dfsc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ResolveInfo;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.AdapterView.OnItemClickListener;

public class RelayProcess extends CommonProcess {

	protected List<Map<String, Object>> rlist = null;
	private Integer switchID = 0;
	
	public RelayProcess(Activity context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void setLayout() {
		// TODO Auto-generated method stub
		setViews();
		RelativeLayout mRelativeLayout = (RelativeLayout) context
				.findViewById(R.id.relativeLayout2);
		
		LinearLayout mainLayout= (LinearLayout) context
				.findViewById(R.id.mainLinearLayout);
		mRelativeLayout.setVisibility(View.GONE);
		//mainLayout.removeView(mRelativeLayout);
		
		mRelativeLayout = (RelativeLayout) context
				.findViewById(R.id.relativeLayout3);
		mRelativeLayout.setVisibility(View.GONE);
		//mainLayout.removeView(mRelativeLayout);
		
		LayoutInflater inflater = (LayoutInflater) context
				.getApplicationContext().getSystemService(
						Context.LAYOUT_INFLATER_SERVICE);
		//final RelativeLayout myLayout = (RelativeLayout) inflater.inflate(
		//		R.layout.setting, null);
		final View relayView = inflater.inflate(R.layout.gshrelay, null);
		mainLayout.addView(relayView);
		
		rlist = getData();
		GridView gridview = (GridView)context.findViewById(R.id.gridView1);
		gridview.setAdapter(new AppsAdapter());

		 gridview.setOnItemClickListener(new OnItemClickListener(){   
	        	public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {   
	        		switchID = (Integer) getData().get(position).get("switch");
	        	}
		 });
	}

	@Override
	protected List<Map<String, Object>> getData() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		/*HrisCommObject[] lightList = GSmartHome.h1000Data.getDeviceList()
				.getLightList();
		if (lightList == null)
			return list;*/
		//for (int i = 0; i < this.relayCount; i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("title", context.getString(R.string.switch1));
				map.put("switch", 1);
				map.put("img", R.drawable.freshair);
				list.add(map);
				map = new HashMap<String, Object>();
				map.put("title", context.getString(R.string.switch2));
				map.put("switch", 2);
				map.put("img", R.drawable.hotwater);
				list.add(map);
				map = new HashMap<String, Object>();
				map.put("title", context.getString(R.string.switch3));
				map.put("switch", 3);
				map.put("img", R.drawable.alarm);
				list.add(map);
				map = new HashMap<String, Object>();
				map.put("title", context.getString(R.string.switch4));
				map.put("switch", 4);
				map.put("img", R.drawable.switch1);
				list.add(map);
				map = new HashMap<String, Object>();
				map.put("title", context.getString(R.string.switch5));
				map.put("switch", 10);
				map.put("img", R.drawable.switch1);
				list.add(map);
				map = new HashMap<String, Object>();
				map.put("title", context.getString(R.string.switch6));
				map.put("switch", 11);
				map.put("img", R.drawable.switch1);
				list.add(map);
				map = new HashMap<String, Object>();
				map.put("title", context.getString(R.string.switch7));
				map.put("switch", 12);
				map.put("img", R.drawable.switch1);
				list.add(map);
				map = new HashMap<String, Object>();
				map.put("title", context.getString(R.string.switch8));
				map.put("switch", 13);
				map.put("img", R.drawable.switch1);
				list.add(map);
			//}
		//}

		return list;
	}

	public class AppsAdapter extends BaseAdapter {
        public AppsAdapter() {
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            
            if (convertView == null) {
            	LayoutInflater inflater = (LayoutInflater) context
        				.getApplicationContext().getSystemService(
        						Context.LAYOUT_INFLATER_SERVICE);
            	convertView = inflater.inflate(R.layout.gshrelayitem, null);
            	
            } else {
            	;
            }
            ((TextView) convertView.findViewById(R.id.relayName)).setText((String)rlist.get(position).get("title"));
            ((TextView) convertView.findViewById(R.id.relayId)).setText( ((Integer)rlist.get(position).get("switch")).toString() );
            ((ImageView) convertView.findViewById(R.id.relayImage)).setImageResource((Integer) rlist.get(position).get("img"));
            
            Button bt = ((Button)convertView.findViewById(R.id.buttonOn));
            
            bt.setOnClickListener(new Button.OnClickListener() {
    			public void onClick(View v) {
    				onoff = 1;
    				
    				switchID =  Integer.parseInt(((TextView)((View)v.getParent()).findViewById(R.id.relayId)).getText().toString());
    				Log.d("honey-TEST", "relay on switchID = " +  switchID);
    				processCmd();
    			}
    		});
            
            bt = ((Button)convertView.findViewById(R.id.buttonOff));
            
            bt.setOnClickListener(new Button.OnClickListener() {
    			public void onClick(View v) {
    				onoff = 0;
    				switchID =  Integer.parseInt(((TextView)((View)v.getParent()).findViewById(R.id.relayId)).getText().toString());
    				Log.d("honey-TEST", "relay off switchID = " +  switchID);
    				processCmd();
    			}
    		});
            //ResolveInfo info = mApps.get(position);
            //i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));

            return convertView;
        }


        public final int getCount() {
            return rlist.size();
        }

        public final Object getItem(int position) {
            return rlist.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }
	
	protected void processCmd() {

		context.showDialog(PROCESS_DIALOG);
		// if (true) return;
		CmdThread ct = new CmdThread();
		if (ct == null)
			return;
		ct.start();
		context.registerReceiver(processCmdReceiver,
				new IntentFilter("dfsc.processCMD"));
	}

	private class CmdThread extends Thread {
		public void run() {

			// todo
			int result = SmartHomeEngine.getInstance().sendCmdRelayCfg(
					switchID,onoff);

			Intent intent = new Intent();
			intent.setAction("dfsc.processCMD");
			intent.putExtra("result", result);
			context.sendBroadcast(intent);
			if (true)
				return;

		}
	}
}
